package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseFragment;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.common.dto.OrderListDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.FourOrderAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.views.RecyclerViewSpacesItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FourOrderFragment extends BaseFragment implements CustomItemClickListener {
    private EditText etSearch;
    private ImageView ivSearch;
    private RecyclerView recyclerview;
    private FourOrderAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private List<OrderDto> datas;
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;

    @Override
    protected void loadData() {
        showLoadingDialog();
        pageNum = 1;
        queryOrderList();
    }

    private Integer pageNum = 1;
    private Integer pageSize = 20;

    /**
     * 获取订单列表
     */
    private void queryOrderList() {
        orderViewModel.queryOrderList(pageNum, pageSize, 8, queryKey);
        orderViewModel.getOrderliveData().observe(this, new Observer<BaseDto<OrderListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<OrderListDto> dto) {
                hideLoadingDialog();
                srNoData.finishRefresh();
                srHasData.finishRefresh();
                srHasData.finishLoadMore();
                //有数据
                if (dto.isSuccess() && EmptyUtils.isNotEmpty(dto.getData().getList())) {
                    srNoData.setVisibility(View.GONE);
                    srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        datas.clear();
                    }
                    datas.addAll(dto.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    //说明是上拉加载
                    if (pageNum > 1) {
                        pageNum--;
                    } else {
                        //第一次就没数据
                        srNoData.setVisibility(View.VISIBLE);
                        srHasData.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    private OrderViewModel orderViewModel;

    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fourorder;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        etSearch = (EditText) view.findViewById(R.id.et_search);
        ivSearch = (ImageView) view.findViewById(R.id.iv_search);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        srHasData = (SmartRefreshLayout) view.findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) view.findViewById(R.id.sr_no_data);
        srHasData.setVisibility(View.GONE);
        srNoData.setVisibility(View.VISIBLE);
        srNoData.setEnableLoadMore(false);
        linearLayoutManager = new LinearLayoutManager(getParentFragment().getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        map.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, DisplayUtil.dip2px(getParentFragment().getActivity(), 10));//下间距
        recyclerview.addItemDecoration(new RecyclerViewSpacesItemDecoration(map));
        adapter = new FourOrderAdapter(getParentFragment().getActivity(), datas, this);
        recyclerview.setAdapter(adapter);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        //RxJava过滤操作符的应用-实时搜索功能
        RxTextView.textChanges(etSearch)
                //监听输入完200毫秒之后发送事件
                .debounce(200, TimeUnit.MILLISECONDS)
                //跳过输入框EditText 初始化的的时候产生的事件。
                .skip(1)
                //把观察者切换到UI线程
                .observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())
                .subscribe(new rx.Observer<CharSequence>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CharSequence charSequence) {
                        pageNum = 1;
                        queryKey = etSearch.getText().toString();
                        queryOrderList();


                    }
                });
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                queryOrderList();
            }
        });
        srHasData.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                queryOrderList();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                queryOrderList();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(getParentFragment().getActivity(), FourOrderDetailActivity.class);
        intent.putExtra("order", datas.get(position));
        startActivity(intent);

    }
}
